package com.mbyte.easy.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.qcbyclass.entity.QcbyClass;
import com.mbyte.easy.qcbyclass.service.IQcbyClassService;
import com.mbyte.easy.qcbyfile.entity.QcbyFile;
import com.mbyte.easy.qcbyfile.service.IQcbyFileService;
import com.mbyte.easy.security.entity.SysRole;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.student.entity.Student;
import com.mbyte.easy.student.service.IStudentService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import com.mbyte.easy.util.Utility;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;

import static java.lang.Long.parseLong;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/student/student")
public class StudentController extends BaseController  {

    private String prefix = "student/student/";
    private String prefix1 = "qcbyfile/qcbyFile/";

    @Autowired
    private IStudentService studentService;
    @Autowired
    private IQcbyClassService qcbyClassService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IQcbyFileService qcbyFileService;
    @Autowired
    private IUserService userService1;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param student
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, Student student) {
        Page<Student> page = new Page<Student>(pageNo, pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
        SysUser sysUser = Utility.getCurrentUser();
        if(sysUser != null){
            Long roleId = sysUser.getRoles().get(0).getId();
        }

        if(!ObjectUtils.isEmpty(student.getUserId())) {
            queryWrapper = queryWrapper.like("user_id",student.getUserId());
         }
        if(!ObjectUtils.isEmpty(student.getClassId())) {
            queryWrapper = queryWrapper.like("class_id",student.getClassId());
         }
        if(!ObjectUtils.isEmpty(student.getName())) {
            queryWrapper = queryWrapper.like("name",student.getName());
         }
        if(!ObjectUtils.isEmpty(student.getSex())) {
            queryWrapper = queryWrapper.like("sex",student.getSex());
         }
        if(!ObjectUtils.isEmpty(student.getAge())) {
            queryWrapper = queryWrapper.like("age",student.getAge());
         }
        if(!ObjectUtils.isEmpty(student.getPhone())) {
            queryWrapper = queryWrapper.like("phone",student.getPhone());
         }
        if(!ObjectUtils.isEmpty(student.getEmail())) {
            queryWrapper = queryWrapper.like("email",student.getEmail());
         }

        IPage<Student> pageInfo = studentService.getInfopage(page, queryWrapper);
        model.addAttribute("searchInfo", student);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        model.addAttribute("roleId", sysUser.getRoles().get(0).getId());
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param student
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Student student){
        return toAjax(studentService.save(student));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        Student student = studentService.getById(id);
        QcbyClass qcbyClass = qcbyClassService.getById(student.getClassId());
        SysUser sysUser = userService.selectByPrimaryKey(student.getUserId());
        student.setClassName(qcbyClass.getClassName());
        student.setUserName(sysUser.getUsername());
        model.addAttribute("student", student);
        return prefix+"edit";
    }
    /**
    * 添加
    * @param student
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Student student){
        return toAjax(studentService.updateById(student));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(studentService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(studentService.removeByIds(ids));
    }

    /**
     * 用于查看本班所有人的信息
     * @param model
     * @param pageNo
     * @param pageSize
     * @param id
     * @return
     */
    @RequestMapping("selectAllInfo/{id}")
    public String selectAllInfo(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,@PathVariable("id")Long id){
        Page<Student> page = new Page<Student>(pageNo, pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
        IPage<Student> pageInfo = studentService.getAllStuInfopage(page, queryWrapper,id);
        model.addAttribute("searchInfo", 1);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }
    /**
     * 用于查看档案信息
     *
     *
     */
    @RequestMapping("selectFile/{id}")
    public String selectFile(Model model, @PathVariable("id")Long id) {
        QueryWrapper<QcbyFile> queryWrapper = new QueryWrapper<QcbyFile>();
        Student student = studentService.getById(id);
        SysUser sysUser = userService.selectByPrimaryKey(student.getUserId());
        List<QcbyFile> qcbyFileList = qcbyFileService.list(new LambdaQueryWrapper<QcbyFile>().eq(QcbyFile::getStudentId,sysUser.getUsername()));
        qcbyFileList.sort(Comparator.comparing(QcbyFile::getCreateTime));
        Collections.reverse(qcbyFileList);
        model.addAttribute("qcbyFileList",qcbyFileList);

        return prefix + "list1";
    }
    /**
     *
     * 用于记录档案
     *
     */
    @GetMapping("addBeforeFile/{id}")
    public String addBeforeFile(Model model,@PathVariable("id")Long id){
        Student student = studentService.getById(id);
        SysUser sysUser = userService.selectByPrimaryKey(student.getUserId());
        student.setUserName(sysUser.getUsername());
        model.addAttribute("student",student);

        return prefix + "edit1";
    }
    @PostMapping("addFile")
    @ResponseBody
    public AjaxResult addFile(QcbyFile qcbyFile){
        LocalDateTime time = LocalDateTime.now();

        qcbyFile.setCreateTime(time);
        return toAjax(qcbyFileService.save(qcbyFile));
    }

}

